package com.dunqi.gpm.shunqing.home.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseViewHolder;
import com.dunqi.gpm.shunqing.R;
import com.dunqi.gpm.shunqing.common.base.RecyclerAdapter;
import com.dunqi.gpm.shunqing.common.util.TimeUtil;
import com.dunqi.gpm.shunqing.home.bean.ProjectTimeline;
import com.dunqi.gpm.shunqing.home.fragment.HomeFragment;
import com.dunqi.gpm.shunqing.my.util.UserUtil;

import java.util.List;

/**
 * Created by 空 on 2017/11/21 0021.
 */
public class ProjectTimelineAdapter extends RecyclerAdapter<ProjectTimeline> {

    int[] drawablesRes = new int[5];
    int[] colors = new int[5];
    String[] status = {"未填写", "进展顺利", "基本正常", "进度滞后", "申请结项"};
    String[] audit_state = {"待汇报", "创建", "待审核", "审核通过", "审核未通过", "待结项", "已结项"};

//    0 未填写
//1 进展顺利（绿灯）
//            2 基本正常（黄灯）
//            3 进度滞后（红灯）
//            4 申请结项

    public ProjectTimelineAdapter(Context context, @Nullable List<ProjectTimeline> data) {
        super(R.layout.item_home_project_timeline, data);
        init();
    }

    private void init() {
        drawablesRes[0] = R.mipmap.time1;
        drawablesRes[1] = R.mipmap.time1;
        drawablesRes[2] = R.mipmap.time2;
        drawablesRes[3] = R.mipmap.time3;
        drawablesRes[4] = R.mipmap.time4;

        colors[0] = Color.parseColor("#0099CC");
        colors[1] = Color.parseColor("#0099CC");
        colors[2] = Color.parseColor("#CC9900");
        colors[3] = Color.parseColor("#669900");
        colors[4] = Color.parseColor("#CC6600");

    }

    @Override
    protected void convert(BaseViewHolder helper, final ProjectTimeline item) {
        head(helper, item);

        String period = item.getReportPeriod();
        String reportTypeTxt = item.getReportType() == 1 ? "日报" : "定期汇报";
        helper.setText(R.id.report_time_tv, TimeUtil.getTen(period) + "(" + reportTypeTxt + ")")
                .setText(R.id.progress_tv, item.getCurrentPeriodInvested() + "")
                .setText(R.id.progress_text_tv, item.getProgressDesc())
                .setText(R.id.audit_state_tv, audit_state[item.getAuditStatus() + 1])
                .setText(R.id.pro_name_tv, item.getProjectName());

        int auditStatus = item.getAuditStatus();

        TextView rightArrawTv = helper.getView(R.id.right_arraw_tv);

        String text = "";
        if (HomeFragment.homeMenuId == 1 && UserUtil.userFromRespOrg() && auditStatus <= 0) {
            rightArrawTv.setVisibility(View.VISIBLE);
            text = "汇报";
        } else if (HomeFragment.homeMenuId == 11) {
            rightArrawTv.setVisibility(View.GONE);
        }


        rightArrawTv.setText(text);

    }

    private void head(BaseViewHolder helper, final ProjectTimeline item) {
        int progressType = item.getProgressType();
        final CheckBox expandCb = helper.getView(R.id.expand_cb);
        final View view = helper.getView(R.id.expand_container);
//        TextView timeTv = helper.getView(R.id.time_tv);
//        String time = item.getReportTime();
//        timeTv.setText(TimeUtil.getTen(time));
//        timeTv.setTextColor(colors[progressType - 1]);

        view.setVisibility(expandCb.isChecked() ? View.GONE : View.VISIBLE);
        expandCb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                item.setChecked(isChecked);
                view.setVisibility(isChecked ? View.GONE : View.VISIBLE);
            }
        });


        expandCb.setBackgroundResource(drawablesRes[progressType]);
        TextView stateTv = helper.getView(R.id.state_tv);

        stateTv.setText(status[progressType]);
        stateTv.setTextColor(colors[progressType]);


    }


}
